package com.hiwhitley.himei.contract.model;

import com.hiwhitley.himei.contract.GameContract;
import com.hiwhitley.himei.entity.GameDesc;
import com.hiwhitley.himei.entity.GameHeader;
import com.hiwhitley.himei.entity.GameItem;
import com.hiwhitley.himei.entity.GameList;
import com.hiwhitley.himei.entity.GamePoster;
import com.hiwhitley.himei.network.RetrofitFactory;
import com.hiwhitley.himei.network.service.GameService;
import com.hiwhitley.himei.utils.StrUtil;

import rx.Observable;

/**
 * Created by hiwhitley on 2016/10/11.
 */

public class GameModel implements GameContract.Model {
    private static final String TAG = "GameModel";
    private static GameService sGameService;

    public GameModel() {
        sGameService = RetrofitFactory.getGameService();
    }

    @Override
    public Observable<GameList> getGames(int offset, int limit) {
        return sGameService.getRecommendGames(offset, limit);
    }

    public static GameItem parseDataByType(GameList.Data data, int type) throws Exception {
        if (type == GameItem.TYPE_HEADER) {
            return new GameHeader()
                    .setIcon(data.getIcon().getUrl())
                    .setName(data.getName_cn())
                    .setPublish_time(StrUtil.timeStamp2Date(data.getPublish_time() + ""))
                    .setGid(data.getGid())
                    .setGameName(data.getName_cn());
        }

        if (type == GameItem.TYPE_POSTER) {
            return new GamePoster()
                    .setGamePoster(data.getPoster()).setGid(data.getGid())
                    .setGameName(data.getName_cn());
        }

        return new GameDesc()
                .setBrief(data.getBrief())
                .setFeature(StrUtil.formatFeature(data.getFeature()))
                .setVideo(data.getVideo().get(0).getUrl())
                .setAvatar(data.getReferrer() != null ? data.getReferrer().getAvatar() : data.getEditor().getAvatar())
                .setReferrerName(data.getReferrer() != null ? data.getReferrer().getNickname() : data.getEditor().getNickname())
                .setDownload_url(data.getPacks().get(0).getDownload_url())
                .setGid(data.getGid())
                .setGameName(data.getName_cn());

    }

}
